<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Backend_IndexController
 *
 * @author Eugene
 */
class Backend_IndexController extends Zend_Controller_Action
{

	/**
	 *
	 * @return void
	 */
	public function indexAction()
	{

	}

	/**
	 *
	 * @return void
	 */
	public function loginAction()
	{
		$form = new Backend_Form_Login;

		if ($this->getRequest()->isPost()) {
			if ($form->isValid($this->getRequest()->getPost())) {
				$adapter = new Zend_Auth_Adapter_DbTable(
								$this->_getParam('db'),
								'user',
								'email',
								'password',
								'MD5(?)'
				);

				$adapter->setIdentity($form->getValue('email'));
				$adapter->setCredential($form->getValue('password'));

				$auth = Zend_Auth::getInstance();
				if ($auth->authenticate($adapter)->isValid()) {
					$data = $adapter->getResultRowObject(null, 'password');
					$user = new Application_Model_User((array) $data);
					$auth->getStorage()->write($user);

					return $this->_helper->redirector('index');
				}
			}
		}

		$this->view->form = $form;
	}

	/**
	 *
	 * @return void
	 */
	public function logoutAction()
	{
		Zend_Auth::getInstance()->clearIdentity();
		$this->_helper->redirector('login');
	}

}